<template>
  <!--围栏地图页-->
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">新增围栏</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('/geofence')">返回</el-button>
  </div>
  <div style="width: 100% ;height: 84vh" id="mapContainer">

  </div>
  <el-dialog title="电子围栏名称" v-model="geofenceMapDialogVisible" :before-close="handleMaoDialogClose">
    <el-form >
      <el-form-item label="围栏名称">
        <el-input v-model="geofence.name" placeholder="请输入围栏名称"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleMaoDialogClose">取 消</el-button>
      <el-button type="primary" @click="submitGeofenceData">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>

import router from "@/router";
import {nextTick, onMounted, ref} from "vue";
import {DrawScene, DrawControl, OperateEventType} from 'bmap-draw';
import {ElMessage} from "element-plus";
import qs from "qs";
import axios from "axios";
// import { createRef, useEffect } from 'react';
var map;
const geofenceMapDialogVisible = ref(false);
onMounted(() => {
  //等待dom页面完毕再添加script渲染
  nextTick(() => {
    map = new BMapGL.Map("mapContainer");
    let point = new BMapGL.Point(116.404, 39.915);
    map.centerAndZoom(point, 15);
    // 添加鼠标绘制控件
    //创建绘制场景类,这是使用其他绘制功能的前提,需要导入
    const scene = new DrawScene(map);
    // 创建绘制控件类,并将上方的绘制场景类传入,需要导入
    const drawControl = new DrawControl(scene, {
      //对绘制控件进行配制
      //关闭绘制项文字提示
      enableTips: false,
      //控件位置
      anchor: BMAP_ANCHOR_TOP_RIGHT,
      //设置控件绘制选项功能
      drawingItems: [
        // 'marker',
        // 'polyline',
        // 'polygon',
        'circle',
        'rectangle'
      ]
    });

    //
    scene.addEventListener(OperateEventType.COMPLETE, (event) => {
      console.log(event.target);
      geofenceMapDialogVisible.value = true;
      console.log(geofenceMapDialogVisible.value);
      let overlay = event.target.overlay;
      if (overlay instanceof BMapGL.Circle) {
        geofence.value.position.type = 'circle';
        geofence.value.position.radius = overlay.getRadius();
        geofence.value.position.longitude = overlay.getCenter().lng;
        geofence.value.position.latitude = overlay.getCenter().lat;
        console.log(geofence.value);
      } else {
        geofence.value.position.type = 'rectangle';
        let points = [];
        for (let i = 0; i < 4; i++) {
          points.push(overlay.points[i].latLng.lng + '-' + overlay.points[i].latLng.lat);
        }
        geofence.value.position.points = points.join(',');
        console.log(geofence.value);
      }

    })
    var scaleCtrl = new BMapGL.ScaleControl();  // 添加比例尺控件
    map.addControl(scaleCtrl);
    var zoomCtrl = new BMapGL.ZoomControl();  // 添加缩放控件
    map.addControl(zoomCtrl);
    var cityCtrl = new BMapGL.CityListControl();  // 添加城市列表控件
    map.addControl(cityCtrl);
    //将绘制控件添加到地图中
    map.addControl(drawControl);
  })
})
const geofence = ref(
    {
      // 围栏名称
      name: '',
      //位置
      position: {},

    }
);
const handleMaoDialogClose = () => {
  if (confirm('您确认要关闭本弹窗吗?')) {
    geofenceMapDialogVisible.value = false;
    geofence.value.name = '';
    geofence.value.position = {};
    map.clearOverlays();
  }
  // router.push('/geofence');
}

const submitGeofenceData = () => {
  if (!geofence.value.name||geofence.value.name.trim()=='') {
    ElMessage.error("请输入围栏名称");
    return;
  }
  console.log(geofence.value);
  geofenceMapDialogVisible.value = false;
  geofence.value.position = JSON.stringify(geofence.value.position);
  // fetch
  const data =qs.stringify(geofence.value);
  axios.post(BASE_URL+'/v1/geofence/add',data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success(response.data.msg);
      geofence.value.name = '';
      geofence.value.position = {};
      // map.clearOverlays();
      // router.push('/geofence');
    }else {
      ElMessage.error(response.data.msg);
    }
  })

}

</script>

<style scoped>

</style>